package com.sun.base;

import android.app.TabActivity;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.eventadapter.GmAdWhirlEventAdapterData;
import com.sun.lotter.R;

@SuppressWarnings("deprecation")
public abstract class BaseTabActivity extends TabActivity {
	public TextView titleView = null; // title display .if it is null,the
										// display use default title;
	public boolean noTitle = true;// 是否需要title bar 默认是需要
	public LinearLayout mainLayout = null;// 主布局
	public RelativeLayout titleLayout = null;// title布局
	public Handler handler;
	private boolean landscape = false;
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (landscape) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		mainLayout = new LinearLayout(this);
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		handler = new Handler();
		initTitle();
		
		mainLayout.addView(
				LayoutInflater.from(this).inflate(R.layout.maintabs, null),
				new LinearLayout.LayoutParams(-1, -1));
		this.setContentView(mainLayout);
		
		init();
		RadioGroup group = (RadioGroup) findViewById(R.id.main_radio);
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int index = 0;
				switch (checkedId) {
				case R.id.radio_button0:
					index = 0;
					break;
				case R.id.radio_button1:
					index = 1;
					break;
				case R.id.radio_button2:
					index = 2;
					break;
				case R.id.radio_button3:
					index = 3;
					break;
				}
				getTabHost().setCurrentTab(index);
				onClickBottom(index);
			}
		});
		group.check(R.id.radio_button0);
		RelativeLayout adLayout = (RelativeLayout) findViewById(R.id.adlayout);
		AdWhirlLayout adView = new AdWhirlLayout(this,GmAdWhirlEventAdapterData.CONST_STR_APPID_ADWHIRL,handler);
		adLayout.addView(adView);
	}

	private void initTitle() {
		RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(
				-1, -2);
		lParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		titleView = new TextView(this);
		titleView.setGravity(Gravity.CENTER);
		titleView.setTypeface(Typeface.DEFAULT_BOLD);
		titleView.setTextSize(20);
		titleView.setPadding(0, 3, 0, 3);
		titleLayout = new RelativeLayout(this);
		titleLayout.addView(titleView, lParams);
		mainLayout.addView(titleLayout, new LinearLayout.LayoutParams(-1, -2));
	}

	@Override
	public void setTitle(CharSequence title) {
		super.setTitle(title);
		if (titleView != null) {
			titleView.setText(title);
		}
	}

	public abstract void init();

	public abstract void onClickBottom(int bottomIndex);

}
